package com.zhehekeji.opsys.api.webservice.server;

import javax.jws.WebService;

import org.apache.cxf.interceptor.InInterceptors;
import org.apache.cxf.interceptor.OutInterceptors;
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhehekeji.opsys.api.code.LuckyVoteResultCode;
import com.zhehekeji.opsys.api.resp.GetDayVoteRecordCountResp;
import com.zhehekeji.opsys.api.resp.GetPeriodVoteRecordCountResp;
import com.zhehekeji.opsys.api.resp.GetVoteListResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerCountOnlyResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerListOnlyResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerListResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerResp;
import com.zhehekeji.opsys.api.resp.GetVoteQrCodeDetailResp;
import com.zhehekeji.opsys.api.resp.GetVoteRecordListResp;
import com.zhehekeji.opsys.api.resp.GetVoteRegionDetailResp;
import com.zhehekeji.opsys.api.resp.GetVoteRegionListResp;
import com.zhehekeji.opsys.api.resp.GetVoteResp;
import com.zhehekeji.opsys.api.resp.LuckyVoteResp;
import com.zhehekeji.opsys.api.resp.VerifyInviteCodeResp;
import com.zhehekeji.opsys.api.service.manager.VoteManagerService;

/**
 * @ClassName VoteManagerWS
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-3-16   下午2:00:08
 */
@InInterceptors(classes={GZIPInInterceptor.class})
@OutInterceptors(classes={GZIPOutInterceptor.class})
@WebService(endpointInterface="com.zhehekeji.opsys.api.webservice.server.VoteManager",
name="voteManager",portName="voteManagerPort",serviceName="voteManagerWS")
public class VoteManagerWS implements VoteManager {
	
	private static final Logger log = LoggerFactory.getLogger(VoteManagerWS.class);
	@Autowired
	private VoteManagerService voteManagerService;
	
	@Override
	public GetVotePlayerListResp getVotePlayerList(String voteCode, String regionCode, int status,
                                                   String sortName, String sortOrder, int firstIndex, int maxResult) {
		return voteManagerService.getVotePlayerList(voteCode, regionCode, status, sortName, sortOrder, firstIndex, maxResult);
	}

	@Override
	public GetVotePlayerListOnlyResp getVotePlayerListOnly(String voteCode, String regionCode, int status,
			String sortName, String sortOrder, int firstIndex, int maxResult) {
		return voteManagerService.getVotePlayerListOnly(voteCode, regionCode, status, sortName, sortOrder, firstIndex, maxResult);
	}

	@Override
	public GetVotePlayerListOnlyResp getVotePlayerListOnlyByVoteId(long voteId, int firstIndex, int maxResult) {
		return voteManagerService.getVotePlayerListOnlyByVoteId(voteId, firstIndex, maxResult);
	}

	@Override
	public GetVotePlayerCountOnlyResp getVotePlayerCountOnly(String voteCode, String regionCode, int status) {
		return voteManagerService.getVotePlayerCountOnly(voteCode, regionCode, status);
	}

	@Override
	public LuckyVoteResp addUserVote(String voteCode, String userID, String productID, int epgType) {
		return voteManagerService.addUserVote(voteCode, userID, productID, epgType);
	}

	@Override
	public LuckyVoteResp reduceUserVote(String voteCode, String userID, String productID, int epgType) {
		return voteManagerService.reduceUserVote(voteCode, userID, productID, epgType);
		
	}

	@Override
	public LuckyVoteResp getVoteCount(String voteCode, String userID, int epgType) {
		return voteManagerService.getVoteCount(voteCode, userID, epgType);
	}

	@Override
	public GetDayVoteRecordCountResp getDayVoteRecordCount(String voteCode, String userID, String playerCode) {
		return voteManagerService.getDayVoteRecordCount(voteCode, userID, playerCode);
	}

	@Override
	public GetPeriodVoteRecordCountResp getPeriodVoteRecordCount(String voteCode, String userID, String playerCode) {
		return voteManagerService.getPeriodVoteRecordCount(voteCode, userID, playerCode);
	}
	
	@Override
	public LuckyVoteResp luckyVote(String playerCode, String userID, int epgType, int votes, String areaId) {
		LuckyVoteResp resp = new LuckyVoteResp();
		try{
			resp = voteManagerService.luckyVote(playerCode, userID, epgType, votes, areaId);
		}catch(Exception e){
			//由javax.persistence.OptimisticLockException这里共享锁抛出的异常，再执行一次
			try{
				log.info(String.format("用户[%s]参与选手[%s]投票时异常，事务回滚，程序自动控制，再投一次！", userID, playerCode));
				resp = voteManagerService.luckyVote(playerCode, userID, epgType, votes, areaId);
			}catch(Exception e2){
				String errorDesc = String.format("用户[%s]参与选手[%s]投票时异常，事务回滚，投票失败！", userID, playerCode);
				resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
				resp.setResultDesc(errorDesc);
				log.error(errorDesc);
			}
		}
		
		return resp;
	}

	@Override
	public GetVotePlayerListResp getMonthChampion(String voteCode, int whichYear, int firstIndex, int maxResult) {
		
		return voteManagerService.getMonthChampion(voteCode, whichYear, firstIndex, maxResult);
		
	}

	@Override
	public GetVotePlayerListResp getMonthRank(String voteCode, int whichYear, int whichMonth, int firstIndex, int maxResult) {
		
		return voteManagerService.getMonthRank(voteCode, whichYear, whichMonth, firstIndex, maxResult);
		
	}

	@Override
	public GetVotePlayerListResp getWeekChampion(String voteCode, int firstIndex, int maxResult) {
		
		return voteManagerService.getWeekChampion(voteCode, firstIndex, maxResult);
		
	}

	@Override
	public GetVotePlayerResp getVotePlayer(String playerCode, int type) {
		
		return voteManagerService.getVotePlayer(playerCode, type);
		
	}

	@Override
	public GetVoteRegionListResp getVoteRegionList(String regionCode, int retVotes, String voteCode, int status, int firstIndex, int maxResult) {
		
		return voteManagerService.getVoteRegionList(regionCode, retVotes, voteCode, status, firstIndex, maxResult);
		
	}

	@Override
	public GetVoteRegionDetailResp getVoteRegionDetail(String regionCode, int retVotes, String voteCode) {
		
		return voteManagerService.getVoteRegionDetail(regionCode, retVotes, voteCode);
		
	}

	@Override
	public VerifyInviteCodeResp verifyInviteCode (String qrCode, String inviteCode, String userID){
		return voteManagerService.verifyInviteCode(qrCode, inviteCode, userID);
	}

	@Override
	public GetVoteQrCodeDetailResp getVoteQrCodeDetail(String qrCode) {
		
		return voteManagerService.getVoteQrCodeDetail(qrCode);
	}

	@Override
	public GetVotePlayerResp searchVotePlayer(String voteCode, String playerNo, int type) {
		
		return voteManagerService.searchVotePlayer(voteCode, playerNo, type);
	}

	@Override
	public GetVotePlayerListResp searchVotePlayerList(String voteCode, String playerNo, String sortName,
			String sortOrder,int type, int firstIndex, int maxResult) {
		
		return voteManagerService.searchVotePlayerList(voteCode, playerNo, sortName, sortOrder, type, firstIndex, maxResult);
	}

	@Override
	public GetVoteResp getVote(String voteCode) {
		return voteManagerService.getVote(voteCode);
	}

	@Override
	public GetVoteListResp getVoteList(Long cpCode) {
		return voteManagerService.getVoteList(cpCode);
	}

	@Override
	public GetVoteRecordListResp getVoteRecordList(String voteCode, String playerCode, String userID,
			String sortName, String sortOrder, int firstIndex, int maxResult) {
		return voteManagerService.getVoteRecordList(voteCode, playerCode, userID, sortName, sortOrder, firstIndex, maxResult);
	}

}

